

function navegation(e, id, arg)

	-- debug session :-)
	if(arg ~= nil) then
		gui.alert('e '..e..' - id '..id..'- arg '..arg)
	else
		gui.alert('e '..e..' - id '..id)
	end -- end of runtime debug section :(


	-- ok lets a basic menu navegate...
	if(e == 21 and id == 1) then
		vtgui:ChoseTest()
	elseif(e== 21 and id == 2) then
		vtgui:lastResults()
	elseif(e == 21 and id == 3) then
		vtgui:resultSumary()
	elseif(e == 21 and id ==4) then
		vtgui:about()
	end

	
	-- the user chose a test in list at home screen
	if(e == 12 and id == 1800) then
		-- pass to chosedDict the value of arg
		chosedDict = arg
		-- go to start a game :)
		vtgui:testScreen()
	end


	-- if e==9, so the user are at testScreen
	if(e==9) then
		if(id==1806) then
			-- exit pressed
			vtgui:lastResults()

		elseif(id == 1802) then
			-- verify pressed
			ut:verify()

		elseif(id == 1803) then
			-- next pressed
			ut:getNewWord()

		elseif(id == 1804) then
			-- solve pressed
			ut:solve()

		elseif(id == 1805) then
			-- skip pressed
			ut:skip()
		end

	end

	

end




ut = {}

function ut:getTitles()
	num = table.getn(dicts)
	
	index=0
	for i=0,num do -- each table index
		for title in string.gfind(dicts[i], "(%S+-%S+)") do
			index = index +1			
			titles[index] = title
			-- print(title)
		end
	end
end

function ut:getNewWord()

	return "hause","casa"
end


function ut:verify()

	if(correctAnswer == gui.gettext(userText)) then
		gui.alert("OK! Correct!")
	else
		gui.alert("Error... wrong answer")
	end
end


function ut:solve()
	gui.alert(correctAnswer)
end



function ut:skip()

end
-- this file draw a user interface and link objects with functions

vtgui = {}

function vtgui:Menus()
	gui.title("SourceDreamsVocabularyTest")
	menus = {"Home",
		 "Last test results",
		 "Results sumary",
		 "About this program"}
	gui.menu(menus)
	-- link menu events too navegation function
	gui.sethandler(menuSelect,navegation)
end


function vtgui:ChoseTest()
	gui.destroy()
	screen.clear()
	vtgui:Menus()
	
	gui.nl()
	print("Select the test that you want play")
	gui.nl()

	ut:getTitles() -- set a list of lessons in titles global..

	-- list component
	gui.control{type="list",lines=10, columns=31,list=titles,handler=navegation}

end


function vtgui:testScreen()
	gui.destroy()
	screen.clear()
	vtgui:Menus()
	
	print("Translate...")
	gui.nl()
	-- the first word of test are geted now..
	RandonWord,correctAnswer = ut:getNewWord()
	screen.font(1) -- set as bold
	gui.tab()
	question = gui.label(RandonWord)

	gui.nl()
	userText=gui.field(1,30,100)
	gui.setfocus(userText)
	gui.nl()


	gui.nl()
	gui.nl()
	screen.font(0)
	gui.control{type="button", width=90, text="Verify", handler=navegation}
	gui.control{type="button", width=60, text="Next", handler=navegation}
	gui.nl()
	gui.control{type="button", width=90, text="Solve", handler=navegation}
	gui.control{type="button", width=60, text="Skip", handler=navegation}
	gui.nl()
	gui.nl()
	gui.nl()
	gui.tab(16)
	gui.control{type="button", text="Exit", handler=navegation}

end



function vtgui:lastResults()
	gui.destroy()
	screen.clear()
	vtgui:Menus()
end



function vtgui:resultSumary()
	gui.destroy()
	screen.clear()
	vtgui:Menus()

end



function vtgui:about()
	gui.destroy()
	screen.clear()
	vtgui:Menus()

	text = "This program is a free sofware! \nGPL v2\n"
	text = text.."Developer: Helton Eduardo Ritter\n"
	text = text.."e-mail: heltonritter@gmail.com\n\n"
	text = text.."site: http://code.google.com/p/code4funfiles/\n\n"
	text = text.."Thanks for use :)\n\n "
	text = text.."* Use the menu to back to home screen"	
	
	gui.control{type="field", lines=11, columns=30, text=text}
end


-- global variables
dicts = {} -- all good strings readed from MemoDB
titles = {} -- titles of tests, readed at MemoDB

chosedDict = -1 -- home screem, user will select

usedWords={} -- used at tests
question = 0 -- question to user answer it

ind = -1
userText = 0 -- the field that is a user answer id of component
RandonWord = "" -- word for this question
correctAnswer = "" -- correct answer for this question



-- read the relacted memo files to vocabulary

f,n = io.open("db:/MemoDB", "r")

-- here is all data of all memo vocabulary registers
-- read all memos in palm
for i = 0,n-1,1 do
	f:openrec(i)
	s = f:read("*a") -- read al document

	-- if the first 5 characters of register is related to sd-vt
	if(string.sub(s,0,5) == "sd#vt") then
		ind = ind + 1		
		dicts[ind] = s
	end
end

-- ok, now dicts are the data of all dicts...

-- call the first UI screen
vtgui:ChoseTest()
gui.main()
